package com.bafa.front.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bafa.exceptions.ErrorException;
import com.bafa.front.DaddyServlet;
import com.bafa.front.utils.FrontUtils;
import com.bafa.objects.AdminUser;
import com.bafa.objects.User;
import com.bafa.services.results.ResultServices;

public class AddResultServlet extends AdminServlet {

	@Override
	public void doAdminEvent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ErrorException {
		HttpSession session = request.getSession();
		
			AdminUser user = (AdminUser)FrontUtils.getUser(request);
			int matchId = new Integer(request.getParameter("matchId")).intValue();
			int teamA = new Integer(request.getParameter("teamA")).intValue();
			int teamB = new Integer(request.getParameter("teamB")).intValue();
			int divisionId = new Integer(request.getParameter("divisionId")).intValue();
			
			if(request.getParameter("walkoverA")!=null){
			
				message = ResultServices.addResult(matchId,3,0,null,null,null,null,teamA,teamB,true,divisionId);
			}else if(request.getParameter("walkoverB")!=null){
				message = ResultServices.addResult(matchId,0,3,null,null,null,null,teamA,teamB,true,divisionId);
			}else{
			
			ArrayList scorersA = new ArrayList();
			ArrayList scoredGoalsA = new ArrayList();
			ArrayList scorersB = new ArrayList();
			ArrayList scoredGoalsB = new ArrayList();
			
			Enumeration enumeration = request.getParameterNames();
			int goalsScoredA = 0;
			int goalsScoredB = 0;
			while(enumeration.hasMoreElements()){
				String param = (String)enumeration.nextElement();
				
				if(param.indexOf("scorerA")>-1){
					String scorerName = request.getParameter(param);
					
					scorersA.add(scorerName);
				}else if(param.indexOf("scoredgoalsA")>-1){
					String scorerGoal = request.getParameter(param);
					goalsScoredA = goalsScoredA+new Integer(scorerGoal).intValue();
					scoredGoalsA.add(scorerGoal);
			    }else if(param.indexOf("scorerB")>-1){
					String scorerName = request.getParameter(param);
					
					scorersB.add(scorerName);
			    }else if(param.indexOf("scoredgoalsB")>-1){
					String scorerGoal = request.getParameter(param);
					goalsScoredB = goalsScoredB+new Integer(scorerGoal).intValue();
					scoredGoalsB.add(scorerGoal);
			    }
					
			}
			
			String[] scorerNamesA = (String[])scorersA.toArray(new String[scorersA.size()]);
			
			String[] scorerGoalsA = (String[])scoredGoalsA.toArray(new String[scoredGoalsA.size()]);
			String[] scorerNamesB = (String[])scorersB.toArray(new String[scorersB.size()]);
			
			String[] scorerGoalsB = (String[])scoredGoalsB.toArray(new String[scoredGoalsB.size()]);
			
			
			int goalsA = new Integer(request.getParameter("goalsA")).intValue();
			int goalsB = new Integer(request.getParameter("goalsB")).intValue();
			String userId = user.getUserId();
			ErrorException error = new ErrorException();
			
			message = ResultServices.addResult(matchId,goalsA,goalsB,scorerNamesA,scorerGoalsA,scorerNamesB,scorerGoalsB,teamA,teamB,false,divisionId);
			}
			url = "logon.bafa";
		
		
	}
}
